package net.learn2develop.GPS;

import net.learn2develop.DB.DatabaseHelper;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PlacesList extends ListActivity {
	
	private Place[] listItems;
	protected ListAdapter adapter;
	protected Cursor cursor;

	 /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places_list);
        
        SQLiteDatabase db = (new DatabaseHelper(getBaseContext())).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id, nameplace, latitud, longitud, address FROM places WHERE categoryId LIKE "+getIntent().getIntExtra("CATEGORY_ID", 1)
        		, new String[]{});
        int i = 0;
		if(cursor.moveToFirst()){
			listItems = new Place [cursor.getCount()];
			do{
				listItems[i] = new Place(cursor.getString(cursor.getColumnIndex("nameplace")), cursor.getString(cursor.getColumnIndex("latitud")), cursor.getString(cursor.getColumnIndex("longitud")),
						cursor.getString(cursor.getColumnIndex("address")), "", "", "", "", "", "", 0, 0, 0,Integer.valueOf(cursor.getString(cursor.getColumnIndex("_id"))));
				i++;
			} while (cursor.moveToNext());
		}
		
		if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
		}
		
		adapter = new PlaceAdapter(PlacesList.this,android.R.layout.simple_list_item_1,listItems);
		setListAdapter(adapter);
		ListView lv = getListView();
        lv.setTextFilterEnabled(true);
    }
	
	public void onListItemClick(ListView parent, View view, int position, long id) {
		
	}
	
	private class PlaceAdapter extends ArrayAdapter<Place> {

        private Place[] places;

        public PlaceAdapter(Context context, int textViewResourceId, Place[] places) {
                super(context, textViewResourceId, places);
                this.places = places;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.place_list_row, null);
                }
                Place o = places[position];
                if (o != null) {
                        TextView tt = (TextView) v.findViewById(R.id.toptext);
                        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                        Button button = (Button) v.findViewById(R.id.button_seemore);
                        if (tt != null) {
                              tt.setText("Name: "+o.getName());                            }
                        if(bt != null){
                              bt.setText("Address: "+ o.getAddress());
                        }
                        if (button != null) {
                        	Button seeMoreButton = (Button) v.findViewById(R.id.button_seemore);
                			seeMoreButton.setOnClickListener(new OnClickListener() {
                 	             public void onClick(View v) {
                 	            	Intent intent = new Intent(PlacesList.this, PlaceInfo.class);
                               		Place place = (Place) adapter.getItem(position);
                                	intent.putExtra("PLACE_ID", place.getIdPlace());
                                	startActivity(intent);
                 	             }
                 	         });
                        }
                }
                return v;
        }
	}
}
